"""
向量化服务
处理文本的向量化操作
"""
from typing import List
from openai import OpenAI
from app.core.config import settings


class EmbeddingService:
    """向量化服务类"""
    
    def __init__(self):
        self.client = OpenAI(
            api_key=settings.OPENAI_API_KEY,
            base_url=settings.OPENAI_BASE_URL
        )
        self.model = settings.EMBEDDING_MODEL
    
    def create_embedding(self, text: str) -> List[float]:
        """
        为单个文本创建向量
        
        Args:
            text: 输入文本
            
        Returns:
            向量列表
        """
        response = self.client.embeddings.create(
            model=self.model,
            input=text
        )
        return response.data[0].embedding
    
    def create_embeddings(self, texts: List[str]) -> List[List[float]]:
        """
        为多个文本批量创建向量
        
        Args:
            texts: 输入文本列表
            
        Returns:
            向量列表的列表
        """
        embeddings = []
        for text in texts:
            embedding = self.create_embedding(text)
            embeddings.append(embedding)
        return embeddings
    
    def chunk_text(self, text: str, chunk_size: int = None) -> List[str]:
        """
        将文本分块
        
        Args:
            text: 输入文本
            chunk_size: 分块大小，默认使用配置值
            
        Returns:
            文本块列表
        """
        if chunk_size is None:
            chunk_size = settings.CHUNK_SIZE
        
        chunks = [text[i:i+chunk_size] for i in range(0, len(text), chunk_size)]
        return chunks


# 全局服务实例
embedding_service = EmbeddingService()

